// import request from '@/utils/request.js'
import { login, getUserInfo, getUserInfoById } from '@/api/user'
import { setToken, getToken, removeToken, setTokenTime } from '@/utils/auth'
const state = {
  token: getToken(),
  userInfo: {}
}
// 获取token
const mutations = {
  setToken(state, payload) {
    state.token = payload
    setToken(payload)
  },
  setUserInfo(state, payload) {
    state.userInfo = payload
  },
  logout(state) {
    state.token = null
    state.userInfo = {}
    removeToken()
  }
}
// 登录
const actions = {
  async login(context, payload) {
    // try  catch  可以在这里写，也可以在调用的地方写
    // try {
    //   const res = await login(payload)
    //   context.commit('setToken', res)
    // } catch (err) {
    //   console.log(err)
    // }

    // console.log(res)
    const res = await login(payload)
    setTokenTime()
    context.commit('setToken', res)
  },
  // 获取用户信息
  async getUserInfo(context) {
    // console.log('获取用户信息')
    const res = await getUserInfo()
    const userId = await getUserInfoById(res.userId)
    // console.log(userId)
    context.commit('setUserInfo', { ...res, ...userId })
    // console.log(res)
  }

}
const getters = {

}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
